<template>
    <div class="components_edit_modal">
        <el-dialog :title="title" 
            :visible.sync="state" :show-close="true" :close-on-click-modal="false">
            <table class="el-table el-table--fit el-table--striped el-table--border el-table--enable-row-transition ">
                <tbody class="el-table__body">
                     <tr>
                        <td width="130px"><label>学校：</label></td>
                        <td width="300px"> {{schoolName}}</td>
                        <td width="130px"><label>学院：</label></td>
                        <td width="300px">{{secondSchoolName}}</td>
                        <td width="130px"><label>专业：</label></td>
                        <td width="300px">{{professionName}}</td>
                    </tr>

                    <tr>
                        <td width="130px"><label>年级：</label></td>
                        <td width="300px">{{form.grade == null ? null : form.grade + "级" }}</td>
                        <td width="130px"><label>开始日期：</label></td>
                        <td width="300px">{{ formatDate(form.startDate) }}</td>
                        <td width="130px"><label>结束日期</label></td>
                        <td width="300px">{{ formatDate(form.endDate) }}</td>
                    </tr>

                    <tr>
                        <td width="130px"><label>学期：</label></td>
                        <td width="300px"> {{form.semester}}</td>
                    </tr>
                </tbody>
            </table>
            <div slot="footer" class="dialog-footer">
                <el-button @click="state = false" size="small">关 闭</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import {enums} from '@/constant'
    import { mapGetters } from 'vuex'
    import moment from 'moment'
    export default {
        data() {
            const validateEndTime = (rule, value, callback) =>{
                if(this.form.endDate <= this.form.startDate){
                    callback(new Error("结束日期必须大于开始日期"));
                }else{
                    callback();
                }
            };
            return {
                calendarId: null,
                form: {
                    schoolId:null,
                    secondSchoolId:null,
                    professionId:null,
                    semester:null,
                    startDate:null,
                    endDate:null
                },
                // 学校列表
                // schoolList:null,
                // 二级院校列表
                secondSchoolList:null,
                // 专业列表
                professionList:null,
                // 学期列表
                semesterList:null,
                // 年级列表
                gradeList: null,
            }
        },
        created() {
            // 获取学校数据
            //this.getSchoolList();
            // 获取学期数据
            this.getSemesterListByDicts();
        },
        mounted() {},
        computed:{
            ...mapGetters({
                schoolList: 'getSchoolList',
            }),
            state: {
                get: function () {
                    if(this.$store.getters.getSchoolCalendarDetail.state == true){
                        let data = this.$store.getters.getSchoolCalendarDetail.data;
                        this.calendarId = data.id;
                        // 根据id查询课程考核方案信息
                        this.getSchoolCalendarInfo();
                    }
                    return this.$store.getters.getSchoolCalendarDetail.state;
                },
                set: function (v) {
                   if(!v){
                        this.$store.commit("SCHOOL_CALENDAR_DETAIL", {dialogState: v, data: null});
                   }
                }
            },
            schoolName(){
                if(this.schoolList == null || this.form.schoolId == null){return;}
                return _.find(this.schoolList, {id: this.form.schoolId}).text;
            },
            secondSchoolName(){
                if(this.secondSchoolList == null || this.form.secondSchoolId == null){return;}
                console.log(this.secondSchoolList,  this.form.secondSchoolId)
                return _.find(this.secondSchoolList, {id: this.form.secondSchoolId}).text;;
            },
            professionName(){
                if(this.professionList == null || this.form.professionId == null){return;}
                return _.find(this.professionList, {id: this.form.professionId}).text;
            },
            title(){
                return this.schoolName + `-` + this.secondSchoolName + `-` + this.professionName 
                    + `-` + this.form.semester
            }
        },
        methods: {
            // 打开模态框
            showDialog(calendarId){
                this.calendarId = calendarId;
                // 获取校历基本信息
                this.getSchoolCalendarInfo();
                // 打开窗口
                this.state = true;
                // 清空表单校验
                this.$nextTick(function(){
                    this.$refs['form'].clearValidate();
                })
            },
            // 获取校历基本信息
            getSchoolCalendarInfo(){
                let vm = this;
                vm.axios.get(api.schoolCalendar.queryById + vm.calendarId).then((response) => {
                    if (response.data.success) {
                        this.form = JSON.parse(JSON.stringify(response.data.data));
                        // 获取二级院系数据
                        this.getSecondSchoolList(this.form.schoolId);
                        // 获取专业列表
                        this.getProfessionList(this.form.schoolId, this.form.secondSchoolId);
                    } else {
                        vm.$message.error(response.data.msg);
                    }
                })
            },
             // 获取学校数据
            getSchoolList() {
                let vm = this;
                vm.axios.get(api.getSchoolList).then((response) => {
                    if (response.data.success) {
                        this.schoolList = JSON.parse(JSON.stringify(response.data.data));
                    } else {
                        vm.$message.error(response.data.msg);
                    }
                })
            },
             // 获取二级院系数据
            getSecondSchoolList(schoolId) {
                let vm = this;
                vm.axios.get(api.getSecondSchoolList,{
                    params: {
                        schoolId: schoolId
                    }
                }).then((response) => {
                    if (response.data.success) {
                        this.secondSchoolList = JSON.parse(JSON.stringify(response.data.data));
                    } else {
                        vm.$message.error(response.data.msg);
                    }
                })
            },
             // 获取二级院系数据
            getProfessionList(schoolId, secondSchoolId) {
                let vm = this;
                vm.axios.get(api.getProfeesionList, {
                     params: {
                        schoolId: schoolId,
                        secondSchoolId: secondSchoolId
                    }
                }).then((response) => {
                    if (response.data.success) {
                        this.professionList = JSON.parse(JSON.stringify(response.data.data));
                    } else {
                        vm.$message.error(response.data.msg);
                    }
                })
            },
             // 查询年级 (来自班级表数据)
            getGradeList(professionId){
                let vm = this;
                vm.axios.get(api.schoolClass.queryGradeList, {
                     params: {
                        professionId: professionId,
                    }
                }).then((response) => {
                    if (response.data.success) {
                        this.gradeList = JSON.parse(JSON.stringify(response.data.data));
                    } else {
                        vm.$message.error(response.data.msg);
                    }
                })
            },
            // 查询学期列表(来自字典)
            getSemesterListByDicts(){
                let vm = this;
                vm.axios.get(api.dictsTypesSubListUrl, {
                     params: {
                        type: "semester_type",
                    }
                }).then((response) => {
                    if (response.data.success) {
                        this.semesterList = JSON.parse(JSON.stringify(response.data.data));
                    } else {
                        vm.$message.error(response.data.msg);
                    }
                })
            },
            
            // 保存
            submit() {
                let vm = this;
                vm.$refs['form'].validate((valid) => {
                    if (valid) {
                        vm.axios.put(api.schoolCalendar.update, vm.form)
                        .then((response) => {
                            if (response.data.success) {
                                vm.$message({
                                    type: 'success',
                                    message: response.data.msg,
                                });
                                // 关闭弹出窗口
                                vm.state = false;
                                // 刷新table
                                vm.$parent.$refs.schoolCalendarTable.$emit('vuetable:refresh');
                            } else {
                                vm.$message.error(response.data.msg);
                            }
                        })
                    } else {
                        return false;
                    }
                });
            },
            changeSchool(schoolId){
                this.form.secondSchoolId = null;
                this.form.professionId = null;
                this.form.grade = null;
                this.getSecondSchoolList(schoolId);
            },
            changeSecondSchool(secondSchoolId){
                this.form.professionId = null;
                this.form.grade = null;
                this.getProfessionList(this.form.schoolId, secondSchoolId);
            },
            changeProfession(professionId){
                this.form.grade = null;
                this.getGradeList(professionId);
            },
            formatDate(value){
                if(!value){return;}
                return moment(value).format('YYYY-MM-DD');
            }
        },
        components: {}
    };
</script>
<style scoped>

</style>